package routers

import (
	"gin.study/middleware"
	"github.com/gin-gonic/gin"
)

//注册每个app中子路由
type Option func(*gin.Engine)

var options = []Option{}

// 注册app的路由配置
func Include(opts ...Option) {
	options = append(options, opts...)
}

// 初始化
func Init() *gin.Engine {
	r := gin.New()
	//gin.DisableConsoleColor()
	// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
	// By default gin.DefaultWriter = os.Stdout
	//r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
	//
	//	// your custom format
	//	return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
	//		param.ClientIP,
	//		param.TimeStamp.Format(time.RFC1123),
	//		param.Method,
	//		param.Path,
	//		param.Request.Proto,
	//		param.StatusCode,
	//		param.Latency,
	//		param.Request.UserAgent(),
	//		param.ErrorMessage,
	//	)
	//}))
	//r.Use(gin.Recovery())
	//注册中间件
	//c.Use(middleware.BeforeHandlers)  //全局注册，所有路由都会使用该中间件，单个路由使用在路由中添加
	//c.Use(middleware.MiddleWare())  //使用闭包需要加()调用
	r.Use(middleware.LoggerToFile())
	for _, opt := range options {
		opt(r)
	}
	return r
}


